<template>
	<view class="content">
		<!-- #ifdef APP-PLUS -->
		<view class="status_bar">
			<view class="top_view">
			</view>
		</view>
		<!-- #endif -->
		<view style="text-align: right;font-size: 14px;margin-right: 10px;color:#666;">中文版</view>
		<view class="login_logo">
			<image class="image" src="../../static/img/logo.png" mode="aspectFit"></image>
			<text style="font-size: 28px;color:#22272B;">环球影投</text>
			<text style="font-size: 10px;color:#22272B;">UNIVERSAL STUDIOS</text>
		</view>
		<view class="input-group">
			<view class="input-row border">
				<image class="image" src="../../static/img/mobile.png" mode="aspectFit"></image>
				<m-input class="m-input" type="text" clearable focus v-model="account" placeholder="请输入手机号码"></m-input>
			</view>
			<view class="input-row">
				<image class="image" src="../../static/img/pwd.png" mode="aspectFit"></image>
				<m-input class="m-input" type="password" displayable v-model="password" placeholder="请输入登录密码"></m-input>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="bindLogin">登录</button>
		</view>
		<view class="action-row">
			<navigator url="../login/reg">注册账号</navigator>
			<navigator url="../login/pwd">忘记密码</navigator>
		</view>

	</view>
</template>

<script>
	import service from '../../service.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import mInput from '../../components/m-input.vue'

	export default {
		components: {
			mInput
		},
		data() {
			return {
				providerList: [],
				hasProvider: false,
				account: '',
				password: '',
				positionTop: 0
			}
		},
		computed: mapState(['forcedLogin']),
		methods: {
			...mapMutations(['login', 'token','inviteCode']),
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			bindLogin() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.account.length < 11) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 11 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}

				const data = {
					mobile: this.account,
					password: this.password
				};
				const _this = this;
				this.$request.post("/api/user/login", {
						data: data,
					})
					.then(res => {
						uni.showToast({
							icon: "none",
							title: res.data.msg
						});
						if (res.data.code == 1) {
							this.toMain(res.data.data.userinfo.token, res.data.data.userinfo.nickname, res.data.data.userinfo.invite_code);
						} else if (res.data.code == 2) {

							uni.reLaunch({
								url: '../login/activation',
							});
						}
					})
					.catch(error => {
						console.error('error:', error);
					});
			},

			toMain(token, userName,inviteCode) {
				this.token(token);
				this.login(userName);
				this.inviteCode(inviteCode);
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					uni.navigateBack();
				}

			}
		},
		onReady() {
			this.initPosition();
			//this.initProvider();
		}
	}
</script>

<style lang="less">
	.content {
		justify-content: space-around;
	}

	.login_logo {
		height: 40%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.image {
			width: 300upx;
			height: 200upx;
		}
	}

	.input-group {
		width: 80%;
		margin: 20upx auto;

		.input-row {
			padding: 20px 0;

			.m-input {
				font-size: 12px;
			}
		}
	}

	.btn-row {}

	.action-row {
		width: 80%;
		margin: 60upx auto;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.action-row navigator {
		color: #1D7BFF;
		font-size: 12px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		width: 100upx;
		height: 100upx;
		border: 1upx solid #dddddd;
		border-radius: 100upx;
		margin: 0 40upx;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 60upx;
		height: 60upx;
		margin: 20upx;
	}
</style>
